﻿function CodeBaseGrid() 
{
    //Module
    Module = "";

    //Module
    ReportName = "";

    //Module
    EntityName = "";

    //var GridViewName
    GridViewName = "";

    //var Key Object
    ObjectKey = "";

    //var URL Action View;Edit;Add;Delete;Save
    UrlView = "";
    UrlEdit = "";
    UrlAdd = "";
    UrlDelete = "";
    UrlSave = "";

    //Validator
    validator = null;

    //Lưới
    gridview = null;

    //Dùng làm cờ để không vào vòng lặp trong sự kiện select change
    flag = 0;

    //Trang thái của trang và dùng để ngăn không cho người dùng thay đổi dòng khi đang ở trạng thái edit hoặc add
    PageState = "Normal";

    //Dòng hiện hành đang sửa
    rowSelectModified = null;

    // giá trị để chọn
    SelectGridValue = "";

    WidthPopup = 500;
    HeightPopup = 200;
    AutoFixResize = false;
    buttonClick = "None";
    objControlsGrid = null;
    oldDataSelect = null;
    this.Init = function (_GridViewName, _ObjectKey, _UrlView, _UrlEdit, _UrlAdd, _UrlDelete, _UrlSave, _Module, _EntityName, _ReportName) {
        GridViewName = _GridViewName;
        ObjectKey = _ObjectKey;
        UrlView = _UrlView;
        UrlEdit = _UrlEdit;
        UrlAdd = _UrlAdd;
        UrlDelete = _UrlDelete;
        UrlSave = _UrlSave;
        Module = _Module;
        EntityName = _EntityName;
        ReportName = _ReportName;
        SelectGridValue = "";
        //Thay đổi trạng thái của toolbar        
        EnableToolBar(PageState);

        if (gridview == null)
            gridview = $('#' + GridViewName).data("kendoGrid");

        if (gridview != null)
            validator = $('#' + GridViewName).kendoValidator().data("kendoValidator");

        //Ngăn không cho thay đổi dòng khi đang edit
        gridview.bind("change", function () {
            if (flag == 0 && PageState != "Normal" && PageState != "NoRecord") {
                flag = 1;
                gridview.select(rowSelectModified);
            }
            flag = 0;

            var selectedRow = this.select();
            var dataItem = this.dataItem(selectedRow);

            if (typeof RowChange != 'undefined' && typeof dataItem != 'undefined') {
                if (oldDataSelect == null || oldDataSelect != dataItem) {
                    RowChange(dataItem);


                    oldDataSelect = dataItem;
                }
            }

            checkToolbarByPermission(PageState);
        });

        gridview.dataSource.bind("change", function (e) {
           
        });

        // select khi refresh
        gridview.bind("dataBound", this.dataBound);

        gridview.bind("edit", this.grid_edit);

        ObjectControlGrid = '{ "data":[' + ObjectControlGrid + ']}';
        objControlsGrid = $.parseJSON(ObjectControlGrid);

        HideBusy("busyMain");

    };

    this.grid_edit = function (e)
    {
        var objKey = e.container.find("input[name=" + ObjectKey + "]");
        

        objKey.unbind().bind('focusout', function () {
            var _code = objKey.val();
            if (_code != null) {
                if (gridview.select().length == 1) {
                    //Get model hiện hành
                    var model = GetCurrentRowEx(gridview);
                    model[ObjectKey] = _code.toUpperCase();
                    objKey.attr("value", _code.toUpperCase());
                }
            }
        });

        if (typeof onGridEdit != 'undefined')
        {
           onGridEdit(e);
        }

        RegisterPropertyChange(e);
    };

    function RegisterPropertyChange(e)
    {
        $(objControlsGrid.data).each(function (i) {
            var o = objControlsGrid.data[i];
            if (o.controltype == "checkbox") {
                var c = e.container.find("input[id=" + o.name + "]");
                if (!IsNull(c)) {
                    c.change(function () {
                        if (typeof onPropertyChanged != 'undefined') {
                            var name = this.id;
                            var check = $(this).is(':checked');
                            onPropertyChanged(name, null, check);
                        }
                    });
                }
            }
            else if (o.controltype == "text") {
                var myInput = document.getElementById(o.name);
                if (!IsNull(myInput)) {
                    attachTextListener(myInput, function () {
                        if (typeof onPropertyChanged != 'undefined') {
                            var name = this.id;
                            var value = this.value;
                            onPropertyChanged(name, null, value);
                        }

                    });
                }
            }
            else if (o.controltype == "numeric") {
                var numerictextbox = $("#" + o.name + "").data("kendoNumericTextBox");
                // bind to the change event
                if (!IsNull(numerictextbox)) {
                    numerictextbox.bind("change", function (e) {
                        if (typeof onPropertyChanged != 'undefined') {
                            var name = this.wrapper.context.id;
                            var old = this._old;
                            var newv = this._value;
                            onPropertyChanged(name, old, newv);
                        }
                    });
                }
            }
            else if (o.controltype == "date") {
                var datePicker = $("#" + o.name + "").data("kendoDatePicker");
                if (!IsNull(datePicker)) {
                    // bind to the change event
                    datePicker.bind("change", function (e) {
                        if (typeof onPropertyChanged != 'undefined') {
                            var name = this.wrapper.context.id;
                            var old = this._old;
                            var newv = this._value;
                            onPropertyChanged(name, old, newv);
                        }
                    });
                }
            }
            else if (o.controltype == "combobox") {
                // get a reference to instance of the Kendo UI ComboBox
                var combobox = $("#" + o.name + "").data("kendoComboBox");
                
                // bind to the change event
                if (!IsNull(combobox)) {
                    //if (!combobox.options.autoBind && !e.model.isNew())
                    //    combobox.input.val(e.model[o.name]);
                    combobox.bind("change", function (e) {
                        var name = this.wrapper.context.id;
                        //var isChange = true;
                        var model = GetCurrentRowEx(gridview);

                        if (model[name] == "[object Object]")
                        {
                            model[name] = this._selectedValue;
                        }

                        //if (model[name] == this._selectedValue) {
                        //    isChange = false;
                        //}

                        if (typeof onPropertyChanged != 'undefined') {
                            var old = this._old;
                            var newv = this._selectedValue;
                            //if (isChange)
                            onPropertyChanged(name, old, newv);
                        }

                        //combox column binding với giá trị null lỗi
                        
                    });
                }
            }

            else if (o.controltype == "lvcombobox") {
                // get a reference to instance of the Kendo UI ComboBox
                var combobox = $("#" + o.name + "").LVComboBox().data("LVComboBox");
                // bind to the change event
                if (!IsNull(combobox)) {
                    combobox.selectchange = function (old, n, m) {
                        if (typeof onPropertyChanged != 'undefined') {
                            var name = o.name;
                            var old = old;
                            var newv = n;
                            onPropertyChanged(name, old, newv);
                        }
                    };

                }
            }
        });
    }

    this.dataBound = function (e)
    {
        if (gridview.dataSource.total() == 0) {
            EnableToolBar("NoRecord");
        }
        else { EnableToolBar("Normal"); }

        if (SelectGridValue != "") {
            SelectRowByCondition(gridview,
                function (e) {
                    return e[ObjectKey] == SelectGridValue;
                });
            SelectGridValue = "";
        }

        else if (SelectGridValue != -1) {
            SelectFirstRow(gridview);
        }

    };

    this.View = function (addOnGrid) {

        if (gridview.select().length == 1) {
            buttonClick = "View";
            //Get model hiện hành
            var model = GetCurrentRowEx(gridview);

            var url = UrlView + '?' + ObjectKey + '=' + model[ObjectKey];

            if (typeof OnChangeUrl != 'undefined') {
                url = OnChangeUrl(url);
            }

            if (AutoFixResize == true)
                ShowWindowCallback(url, 0, 0, 'View ' + $('div.mtitle').html());
            else
                ShowWindowCallback(url, WidthPopup, HeightPopup, 'View ' + $('div.mtitle').html());
        }
    };

    this.Add = function (addOnGrid) {
        if (addOnGrid == "True") {
            gridview.addRow();

            //Chỉ select dòng cuoi

            var rowIndex = gridview.dataSource.data().length-1;
            SelectRowByIndex(gridview, rowIndex);

            rowSelectModified = gridview.select();

            PageState = "Add";
            EnableToolBar(PageState);
        }
        else {
            // Lấy controller
            var url = UrlAdd + "?t=1";

            if (typeof OnChangeUrl != 'undefined') {
                url = OnChangeUrl(url);
            }
            buttonClick = "Add";
            if (AutoFixResize == true)
                ShowWindowCallback(url, 0, 0, 'Add ' + $('div.mtitle').html(), function (result) {
                    if (typeof _codeBaseGrid != 'undefined') {
                        _codeBaseGrid.closeWindow(result.RefreshData, result.Key);
                    }
                });
            else
                ShowWindowCallback(url, WidthPopup, HeightPopup, 'Add ' + $('div.mtitle').html(), function (result) {
                    if (typeof _codeBaseGrid != 'undefined') {
                        _codeBaseGrid.closeWindow(result.RefreshData, result.Key);
                    }
                });
        }
    };

    this.Copy = function (addOnGrid) {
        if (gridview.select().length == 1) {
            if (addOnGrid == "True") {
                var modelToCopy = GetCurrentRowEx(gridview);

                gridview.addRow();

                //Chỉ select dòng cuoi

                var rowIndex = gridview.dataSource.data().length - 1;
                SelectRowByIndex(gridview, rowIndex);

                rowSelectModified = gridview.select();

                var modelCurrent = GetCurrentRowEx(gridview);

                var i = 0;
                for (i = 0; i < objControlsGrid.data.length; i++)
                {
                    var o = objControlsGrid.data[i];
                    if (o.name != ObjectKey) {
                        if (typeof modelCurrent[o.name] != "undefined") {
                            this.SetValue(o.name, modelToCopy[o.name]);
                        }
                    }
                }

                PageState = "Add";
                EnableToolBar(PageState);
            }
            else {
                // Lấy controller
                var model = GetCurrentRowEx(gridview);
                var url = UrlEdit + '?' + ObjectKey + '=' + model[ObjectKey];;

                url = url + "&copy=1";

                if (typeof OnChangeUrl != 'undefined') {
                    url = OnChangeUrl(url);
                }
                buttonClick = "Copy";
                if (AutoFixResize == true)
                    ShowWindowCallback(url, 0, 0, 'Copy ' + $('div.mtitle').html(), function (result) {
                        if (typeof _codeBaseGrid != 'undefined') {
                            _codeBaseGrid.closeWindow(result.RefreshData, result.Key);
                        }
                    });
                else
                    ShowWindowCallback(url, WidthPopup, HeightPopup, 'Copy ' + $('div.mtitle').html(), function (result) {
                        if (typeof _codeBaseGrid != 'undefined') {
                            _codeBaseGrid.closeWindow(result.RefreshData, result.Key);
                        }
                    });

            }
        }
    };

    this.Edit = function (addOnGrid) {
        if (gridview.select().length == 1) {
            if (addOnGrid == "True") {
                rowSelectModified = gridview.select();
                gridview.editRow(gridview.select());
                PageState = "Edit";
                EnableToolBar(PageState);
            }
            else {
                var model = GetCurrentRowEx(gridview);
                var url = UrlEdit + '?' + ObjectKey + '=' + model[ObjectKey];

                if (typeof OnChangeUrl != 'undefined') {
                    url = OnChangeUrl(url);
                }
                buttonClick = "Edit";
                if (AutoFixResize == true)
                    ShowWindowCallback(url, 0, 0, 'Edit ' + $('div.mtitle').html(), function (result) {
                        if (typeof _codeBaseGrid != 'undefined') {
                            _codeBaseGrid.closeWindow(result.RefreshData, result.Key);
                        }
                    });
                else
                    ShowWindowCallback(url, WidthPopup, HeightPopup, 'Edit ' + $('div.mtitle').html(), function (result) {
                        if (typeof _codeBaseGrid != 'undefined') {
                            _codeBaseGrid.closeWindow(result.RefreshData, result.Key);
                        }
                    });

                
            }
        }
    };


    this.OpenWindow = function (addOnGrid, url, Title, WidthPopup, HeightPopup,Para1) {
        if (gridview.select().length == 1) {
            var model = GetCurrentRowEx(gridview);
            url=url.replace("!1", model[ObjectKey]);
            

            if (AutoFixResize == true)
                GolbalMessage.OpenWindowAuto(url, Title);
            else
                GolbalMessage.OpenWindow(url, Title, WidthPopup, HeightPopup);
        }
    };

    this.Delete = function () {
        if (gridview.select().length == 0) return;

        ShowMessage("Question", "DeleteRecode", "Are you sure delete this record?", 
            function (e) {
                if (e == "YES") {
                    var model = GetCurrentRowEx(gridview);

                    var url = UrlDelete;

                    ShowBusy("busyMain");
                    PostModel(url, model, function (Rdata) {
                        HideBusy("busyMain");
                        if (Rdata == 0) {
                            var rowIndex = gridview.dataSource.indexOf(model);
                            //không chọn lại dòng đầu
                            SelectGridValue = -1;
                            gridview.removeRow(gridview.select());
                            if (rowIndex > 0) rowIndex = rowIndex - 1;
                            {
                                var selectModel = gridview.dataSource.at(rowIndex);
                                if (selectModel != null) {
                                    
                                    SelectGridValue = selectModel[ObjectKey];
                                    return;
                                }
                            }
                        }
                        else
                            ShowMessage('Error', '', Rdata);
                    });
                }
            },null,"YesNo");
    };

    this.Save = function () {
        // kiểm tra ràng buộc
        if (!validator.validate()) {
            var errors = validator.errors();
            var errormsg = "";
            $(errors).each(function () {
                errormsg = errormsg + " - " + this;

            });

            ShowMessage("Error", '',errormsg);

            return;
        }

        var model = GetCurrentRowEx(gridview);

        var url = UrlSave + '?Edit=';

        if (PageState == "Edit")
            url = url + 1;
        else
            url = url + 0;

        url = url + "&ID=" + model.id;

        ShowBusy("busyMain");

        PostModel(url, model, function (Rdata) {
            HideBusy("busyMain");

            if (Rdata == 0) {
                gridview.saveRow();

                var medit = 1;
                if (PageState == "Add")
                    medit = 0;

                PageState = "Normal";
                EnableToolBar(PageState);

                //Chọn lại dòng khi đã lưu lại
                if (medit == 1) {
                    if (model.dirty)
                        SelectGridValue = model[ObjectKey];
                    else {
                        var rowIndex = gridview.dataSource.indexOf(model);
                        SelectRowByIndex(gridview, rowIndex);
                    }
                }
                else {
                    //Chỉ select dòng cuoi
                    SelectGridValue = model[ObjectKey];
                }

                var _cancel = false;
                if (typeof GridSaveCompleted != 'undefined')
                    GridSaveCompleted(_cancel);
            }
            else {
                ShowMessage("Error","", Rdata,null,null, "OK");
            }
        });

    };

    this.Undo = function () {
        var model = GetCurrentRowEx(gridview);
        var rowIndex = gridview.dataSource.indexOf(model);

        gridview.cancelRow();
        if (gridview.dataSource.total() == 0) {
            PageState = "NoRecord";
        }
        else {
            PageState = "Normal";
        }
        EnableToolBar(PageState);

        //Chọn lại dòng khi đã khôi phục
        SelectRowByIndex(gridview, rowIndex);
    };

    this.PredicateReport = "";
    this.DataValueReport = "";
    this.Print = function () {
        var url = "ReportViewer.aspx?Module=" + Module + "&EntityName=" + EntityName + "&ReportName=" + ReportName + "&FormTitle=" + $('div.mtitle').html();

        if (this.Predicate != "")
        {
            url = url + "&Predicate=" + this.PredicateReport + "&DataValue=" + this.DataValueReport;
        }

        if (typeof OnBeforePrint != 'undefined') {
            url = OnBeforePrint(url);
        }
        
        window.parent.open(url);
    };

    this.PrintBy = function (Module, dataSet, ReportName, Title, storeName, para) {
        para = para.replace(' ', '%20');

        var url = GetBaseUrl() + "ReportViewer.aspx?Module=" + Module + "&EntityName=" + dataSet + "&ReportName=" + ReportName + "&FormTitle=" + Title + "&StoreProcedure=" + storeName + '&' + para;


        window.parent.open(url);
    };

    this.refreshData = function (key) {
        if(buttonClick == "Add" || buttonClick == "Copy")
        {
            if (!IsNullOrEmty(FieldSort))
            {
                var pagesize = gridview.dataSource.options.pageSize;
                var page = PostServer(GetBaseUrl() + 'Shared/GetPage?Key=' + ObjectKey + '&Value=' + key + '&EntityName=' + TableName + '&Sort=' + FieldSort + '&PageSize=' + pagesize);
                if (!IsNull(page))
                {
                    gridview.dataSource.page(page.PageCurrent);
                    SelectGridValue = key;
                    return;
                }
            }
        }

        gridview.dataSource.read();
        SelectGridValue = key;
    };

    //gọi từ form popup
    this.closeWindow = function (_isRefreshData, key) {
        if (_isRefreshData == 1 && !IsNullOrEmty(key)) {
            this.refreshData(key);
            EnableToolBar("Normal");
        }
    };

    //gọi từ form popup
    this.SaveAddRefresh = function () {
        GolbalMessage.PostAndChangeContent(UrlAdd);
    };


    this.ResizeInnerGrid = function () {
        var grid = $("#" + GridViewName);
        var container = $("#rightContent");
        var newHeight = container.height() - 80;
        var newWidth = container.width() - 10;

        var dataArea = grid.find(".k-grid-content");
        dataArea.height(newHeight - 80);

        grid.height(newHeight);

        if ($("#mnMoreFunctions").val() == null)
            grid.width(newWidth);
        else
            grid.width(newWidth - 190);
    };


    EnableToolBar = function (status) {
        if (status == "NoRecord")
        {
            document.getElementById('btnView').disabled = true;
            document.getElementById('btnAdd').disabled = false;
            document.getElementById('btnCopy').disabled = true;
            document.getElementById('btnEdit').disabled = true;
            document.getElementById('btnDelete').disabled = true;
            document.getElementById('btnUndo').disabled = true;
            document.getElementById('btnSave').disabled = true;
            document.getElementById('btnPrint').disabled = true;


            document.getElementById('btnView').className = "disabled";
            document.getElementById('btnAdd').className = "positive";
            document.getElementById('btnCopy').className = "disabled";
            document.getElementById('btnEdit').className = "disabled";
            document.getElementById('btnDelete').className = "disabled";
            document.getElementById('btnUndo').className = "disabled";
            document.getElementById('btnSave').className = "disabled";
            document.getElementById('btnPrint').className = "disabled";
        }
        else if (status == "Normal") {
            document.getElementById('btnView').disabled = false;
            document.getElementById('btnAdd').disabled = false;
            document.getElementById('btnCopy').disabled = false;
            document.getElementById('btnEdit').disabled = false;
            document.getElementById('btnDelete').disabled = false;
            document.getElementById('btnUndo').disabled = true;
            document.getElementById('btnSave').disabled = true;
            document.getElementById('btnPrint').disabled = false;


            document.getElementById('btnView').className = "positive";
            document.getElementById('btnAdd').className = "positive";
            document.getElementById('btnCopy').className = "positive";
            document.getElementById('btnEdit').className = "positive";
            document.getElementById('btnDelete').className = "positive";
            document.getElementById('btnUndo').className = "disabled";
            document.getElementById('btnSave').className = "disabled";
            document.getElementById('btnPrint').className = "positive";
        }
        else {
            document.getElementById('btnView').disabled = true;
            document.getElementById('btnAdd').disabled = true;
            document.getElementById('btnCopy').disabled = true;
            document.getElementById('btnEdit').disabled = true;
            document.getElementById('btnDelete').disabled = true;
            document.getElementById('btnUndo').disabled = false;
            document.getElementById('btnSave').disabled = false;
            document.getElementById('btnPrint').disabled = true;

            document.getElementById('btnView').className = "disabled";
            document.getElementById('btnAdd').className = "disabled";
            document.getElementById('btnCopy').className = "disabled";
            document.getElementById('btnEdit').className = "disabled";
            document.getElementById('btnDelete').className = "disabled";
            document.getElementById('btnUndo').className = "positive";
            document.getElementById('btnSave').className = "positive";
            document.getElementById('btnPrint').className = "disabled";
        }

        if (typeof CustomEnableToolBar != 'undefined')
            CustomEnableToolBar(status)

        checkToolbarByPermission(status);
    };

    function checkToolbarByPermission(status)
    {

        if (status == "NoRecord" || status == "Normal") {
            if (!CheckPermission("2")) {
                document.getElementById('btnAdd').disabled = true;
                document.getElementById('btnCopy').disabled = true;

                document.getElementById('btnAdd').className = "disabled";
                document.getElementById('btnCopy').className = "disabled";
            }

            if (!CheckPermission("3")) {
                document.getElementById('btnEdit').disabled = true;
                document.getElementById('btnEdit').className = "disabled";
            }

            if (!CheckPermission("4")) {
                document.getElementById('btnDelete').disabled = true;
                document.getElementById('btnDelete').className = "disabled";
            }
        }
    }
    this.GetWinPopup = function ()
    {
        return GolbalMessage;
    }

    this.SetWidthPupop=function(_width)
    {
        WidthPopup = _width;
    }

    this.SetHeightPupop = function (_height)
    {
        HeightPopup = _height;
    }

    this.SetAutoFixSize = function (_AutoFixSize) {
        AutoFixResize = _AutoFixSize;
    }

    this.IsReadOnly = function (fieldName, isReadOnly) {
        $(objControlsGrid.data).each(function (i) {
            var o = objControlsGrid.data[i];
            if (o.name == fieldName) {
                if (o.controltype == "checkbox") {
                    if (isReadOnly == true)
                        $(document.getElementById(o.name)).attr("disabled", "disabled");
                    else
                        $(document.getElementById(o.name)).removeAttr("disabled");
                }
                else if (o.controltype == "text") {
                    if (isReadOnly == true)
                        $(document.getElementById(o.name)).attr("disabled", "disabled");
                    else
                        $(document.getElementById(o.name)).removeAttr("disabled");
                }
                else if (o.controltype == "numeric") {
                    var numerictextbox = $("#" + o.name + "").data("kendoNumericTextBox");

                    // bind to the change event
                    if (isReadOnly == true)
                        numerictextbox.enable(false);
                    else
                        numerictextbox.enable();
                }
                else if (o.controltype == "date") {
                    var datePicker = $("#" + o.name + "").data("kendoDatePicker");
                    if (isReadOnly == true)
                        datePicker.enable(false);
                    else
                        datePicker.enable();
                }
                else if (o.controltype == "combobox") {
                    // get a reference to instance of the Kendo UI ComboBox
                    var combobox = $("#" + o.name + "").data("kendoComboBox");
                    if (isReadOnly == true)
                        combobox.enable(false);
                    else
                        combobox.enable();
                }
                else if (o.controltype == "lvcombobox") {
                    // get a reference to instance of the Kendo UI ComboBox
                    var combobox = $("#" + o.name + "").LVComboBox().data("LVComboBox");
                    if (isReadOnly == true)
                        combobox.enable(false);
                    else
                        combobox.enable(true);
                }
            }
        });
    }

    this.SetValue = function (fieldName, value) {

        var model = GetCurrentRowEx(gridview);
        model[fieldName] = value;


        if (model[fieldName + "_input"] != 'undefined') {
            model[fieldName + "_input"] = value;
        }

        $(objControlsGrid.data).each(function (i) {
            var o = objControlsGrid.data[i];
            if (o.name == fieldName) {
                if (o.controltype == "checkbox") {
                    if (value == true) {
                        $(document.getElementById(o.name)).attr('checked', true);
                    }
                    else {
                        $(document.getElementById(o.name)).attr('checked', false);
                    }

                    
                }
                else if (o.controltype == "text") {
                    $('#' + o.name).attr("value", value);
                }
                else if (o.controltype == "numeric") {
                    var numerictextbox = $("#" + o.name + "").data("kendoNumericTextBox");
                    // bind to the change event
                    numerictextbox.value(value);
                }
                else if (o.controltype == "date") {
                    var datePicker = $("#" + o.name + "").data("kendoDatePicker");
                    datePicker.value(value);
                }
                else if (o.controltype == "combobox") {
                    // get a reference to instance of the Kendo UI ComboBox
                    var combobox = $("#" + o.name + "").data("kendoComboBox");
                    combobox.value(value);
                   
                }
                else if (o.controltype == "lvcombobox") {
                    // get a reference to instance of the Kendo UI ComboBox
                    var combobox = $("#" + o.name + "").LVComboBox().data("LVComboBox");
                    combobox.value(value);
                }
            }
        });
    }

    this.Focus = function (fieldName) {

        $(objControlsGrid.data).each(function (i) {
            var o = objControlsGrid.data[i];
            if (o.name == fieldName) {
                if (o.controltype == "checkbox") {
                        $(document.getElementById(o.name)).focus();
                }
                else if (o.controltype == "text") {
                    $('#' + o.name).focus();
                }
                else if (o.controltype == "numeric") {
                    var numerictextbox = $("#" + o.name + "").data("kendoNumericTextBox");
                    // bind to the change event
                    numerictextbox.focus();
                }
                else if (o.controltype == "date") {
                    var datePicker = $("#" + o.name + "").data("kendoDatePicker");
                    datePicker.focus();
                }
                else if (o.controltype == "combobox") {
                    // get a reference to instance of the Kendo UI ComboBox
                    var combobox = $("#" + o.name + "").data("kendoComboBox");
                    combobox.focus();
                }
                else if (o.controltype == "lvcombobox") {
                    // get a reference to instance of the Kendo UI ComboBox
                    var combobox = $("#" + o.name + "").LVComboBox().data("LVComboBox");
                    combobox.focus();
                }
            }
        });
    }

    this.GetValue = function (fieldName) {
        var returnValue;
        $(objControlsGrid.data).each(function (i) {
            var o = objControlsGrid.data[i];
            if (o.name == fieldName) {
                if (o.controltype == "checkbox") {
                    if ($(document.getElementById(o.name)).attr('checked') == 'checked')
                        returnValue = true;
                    else
                        returnValue = false;

                }
                else if (o.controltype == "text") {
                    returnValue = $('#' + o.name).attr("value");
                }
                else if (o.controltype == "numeric") {
                    var numerictextbox = $("#" + o.name + "").data("kendoNumericTextBox");
                    // bind to the change event
                    returnValue = numerictextbox.value();
                }
                else if (o.controltype == "date") {
                    var datePicker = $("#" + o.name + "").data("kendoDatePicker");
                    returnValue = datePicker.value();
                }
                else if (o.controltype == "combobox") {
                    // get a reference to instance of the Kendo UI ComboBox
                    var combobox = $("#" + o.name + "").data("kendoComboBox");
                    returnValue = combobox.value();
                }
                else if (o.controltype == "lvcombobox") {
                    // get a reference to instance of the Kendo UI ComboBox
                    var combobox = $("#" + o.name + "").LVComboBox().data("LVComboBox");
                    combobox.value();
                }
            }
        });
        return returnValue;
    }

    this.GetValueFromModel = function (fieldName) {
        var model = GetCurrentRowEx(gridview);
        return model[fieldName];
    }

    this.SetPredicateForCombo = function (fieldName, fieldsource, value) {
        $(objControlsGrid.data).each(function (i) {
            var o = objControlsGrid.data[i];
            if (o.name == fieldName) {
                if (o.controltype == "combobox") {
                    // get a reference to instance of the Kendo UI ComboBox
                    var combobox = $("#" + o.name + "").data("kendoComboBox");
                    
                    combobox.dataSource.filter({ field: fieldsource, operator: "IsEqualTo", value: value });
                    
                }
            }
        });
    }

    //Predicates
    //Ex: [{ "field": "VATGroupID", "operator": "contains","value": "1" }, { "field": "ABC", "operator": "contains", "value": "1" }]
    this.SetMultiPredicateForCombo = function(fieldName, Predicates)
    {
        $(objControlsGrid.data).each(function (i) {
            var o = objControlsGrid.data[i];
            if (o.name == fieldName) {
                if (o.controltype == "combobox") {
                    // get a reference to instance of the Kendo UI ComboBox
                    var combobox = $("#" + o.name + "").data("kendoComboBox");
                    var objfilter = $.parseJSON(Predicates);
                    combobox.dataSource.filter(objfilter);
                }
            }
        });
    }

    //codeBasePopup.SetMultiPredicateComplexForCombo("CashAcctID", "(AccountID.IndexOf(~0)==0||AccountID.IndexOf(~1)==0||AccountID.IndexOf(~2)==0)&&!AccountID.Equals(~3)&&Detail.Equals(~4)", "1|4|5|111|true");
    this.SetMultiPredicateComplexForCombo = function (fieldName, Predicates, values) {

        $(objControlsGrid.data).each(function (i) {
            var o = objControlsGrid.data[i];
            if (o.name == fieldName) {
                if (o.controltype == "combobox") {
                    // get a reference to instance of the Kendo UI ComboBox
                    var combobox = $("#" + o.name + "").data("kendoComboBox");

                    combobox.dataSource.filter({ field: Predicates, operator: "IsEqualTo", value: values });

                }
            }
        });

    }

    this.Header = function (field)
    {
        return GetColumnHeader(GridViewName, field);
    }
}